//
//  AppDelegate.m
//  HuLaQuan
//
//  Created by hok on 1/5/16.
//  Copyright © 2016 Jianghao. All rights reserved.
//

#import "AppDelegate.h"
#import "YuHTTPClient.h"
#import <AFNetworkReachabilityManager.h>

#import "YuUserDefaultsUtilities.h"
#import <KVNProgress/KVNProgress.h>

#import <BaiduMapAPI/BMapKit.h>//引入所有的头文件


//网络加载标识
#import "AFNetworkActivityIndicatorManager.h"

//警告框
#import "UIAlertController+CustomAlert.h"

#import "CurrentUser.h"

@interface AppDelegate ()<BMKGeneralDelegate>

//百度地图管理
@property(strong,nonatomic)BMKMapManager *mapManager;

@end

@implementation AppDelegate

- (void)mapInit {
    // 要使用百度地图，请先启动BaiduMapManager
    _mapManager = [[BMKMapManager alloc]init];
    //注册密钥
    BOOL ret = [_mapManager start:@"gAY2ZZEiQ6qliC90W05r7dxg" generalDelegate:nil];
    
    if (!ret) {
        NSLog(@"manager start failed!");
    }
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
//    NSArray * fontArrays = [[NSArray alloc] initWithArray:[UIFont familyNames]];
//    for (NSString * temp in fontArrays) {
//        NSLog(@"Font name  = %@", temp);
//    }
    
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    
    [YuHTTPClient netWorkStatus:^(id status){
        if ([status isEqualToString:@"0"]) {
            UIAlertController *x = [UIAlertController alertWithTitle:@"提示" andMessage:TEXT_NETWORK_ERROR andBlock:^(UIAlertAction *action ) {
                if ([action.title isEqualToString:@"好的"]) {
                    
                }
                
            }  type:@"confirm"];
            [self.window.rootViewController presentViewController:x animated:YES completion:nil];
        }
    }];

    
    [[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];
    
    //百度地图的初始化
    [self mapInit];
    
    
    [self getUDID];
   
    
    
    [self setupKVNProgress];
    // Override point for customization after application launch.
   /* [YuHTTPClient XMLDataWithUrl:@"http://219.140.165.62:8080/custserv/gw" parameters:<#(NSString *)#> success:<#^(BOOL isSuccess, id item)block#> success:^(BOOL isSuccess, id item){
        
        NSDictionary *result;
        if (isSuccess) {
            
        }else{
            
        }
    }];*/

    
    NSLog(@"%@ %@",[CurrentUser sharedInstance].ID,[CurrentUser sharedInstance].token);
    UIStoryboard *storyBoardA = [UIStoryboard storyboardWithName:@"TabBar" bundle:nil];
    UIStoryboard *storyBoardB = [UIStoryboard storyboardWithName:@"LoginAndRegister" bundle:nil];
    self.window.rootViewController = ([CurrentUser sharedInstance].ID && [CurrentUser sharedInstance].token)?  [storyBoardA instantiateViewControllerWithIdentifier:  @"HomeTabBarController"]: [storyBoardB instantiateViewControllerWithIdentifier:@"LoginNavigationController" ];

    
   // NSLog(@"%@",DEVICE_NAME);
    return YES;
}

- (void)getUDID
{
    if (![YuUserDefaultsUtilities valueWithKey:@"UUID"]) {
    NSUUID *UUID = [[UIDevice currentDevice] identifierForVendor];
    NSString *stringUUID = [UUID UUIDString];
    
        [YuUserDefaultsUtilities saveValue:stringUUID forKey:@"UUID"];
    }
}

/**
 *  设置加载遮罩层
 */
- (void)setupKVNProgress
{
    
    [KVNProgressConfiguration defaultConfiguration].backgroundType = KVNProgressBackgroundTypeSolid;
    [KVNProgressConfiguration defaultConfiguration].fullScreen = YES;
    [KVNProgress setConfiguration:[KVNProgressConfiguration defaultConfiguration]];
}
- (void)applicationWillResignActive:(UIApplication *)application {
    [BMKMapView willBackGround];//当应用即将后台时调用，停止一切调用opengl相关的操作
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
     [BMKMapView didForeGround];//当应用恢复前台状态时调用，回复地图的渲染和opengl相关的操作
    
    
    
    
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

#pragma mark - BMKGeneralDelegate
- (void)onGetNetworkState:(int)iError
{
    if (0 == iError) {
        NSLog(@"baidu map api: ---------- ------联网成功");
    }
    else{
        NSLog(@"baidu map api: ---------- ------onGetNetworkState %d",iError);
    }
    
}
//代理方法
- (void)onGetPermissionState:(int)iError
{
    if (0 == iError) {
        NSLog(@"baidu map api: ---------- ------授权成功");
    }
    else {
        NSLog(@"baidu map api: ---------- ------onGetPermissionState %d",iError);
    }
}

@end
